package com.cptravel.activity.hot;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseActivity;
import com.core.util.B5MActivityHelper;
import com.core.util.B5MBitmapHelper;
import com.core.widget.pulltorefresh.PullToRefreshListView;
import com.cptravel.R;
import com.cptravel.activity.MapActivity;
import com.cptravel.activity.VideoActivity;
import com.cptravel.adapter.HotDetailAdapter;
import com.cptravel.api.XiangxiRequest;
import com.cptravel.api.XiangxiResponse;
import com.cptravel.bean.Product;
import com.cptravel.bean.Xiangxi;

import java.util.ArrayList;

public class HotDetailActivity extends B5MBaseActivity implements OnClickListener
{
    private PullToRefreshListView mPullToRefreshListView;
    private HotDetailAdapter mHotDetailAdapter;
    private Product product;
    private View headView;
    private XiangxiRequest mXiangxiRequest;

    @Override protected String getHeadTitle()
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.hot_detail;
    }

    @Override public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        headView = View.inflate(mContext, R.layout.hot_detail_headview, null);
        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        mPullToRefreshListView.getRefreshableView().addHeaderView(headView);
        headView.setOnClickListener(new OnClickListener()
        {
            @Override public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Bundle bundle = new Bundle();
                bundle.putSerializable("product", product);
                B5MActivityHelper.startActivity(mActivity, HotDetailHeadViewDescActivity.class, bundle);
            }
        });
        mPullToRefreshListView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                // TODO Auto-generated method stub
                Xiangxi xiangxi = mHotDetailAdapter.getList().get((int) arg3);
                Bundle bundle = new Bundle();
                bundle.putSerializable("xiangxi", xiangxi);
                B5MActivityHelper.startActivity(mActivity, HotDetailItemDescActivity.class, bundle);
            }
        });
    }

    @Override public void doInitDataes()
    {
        // TODO Auto-generated method stub
        product = (Product) getIntent().getSerializableExtra("product");
        tv_title.setText(product.title);
        mHotDetailAdapter = new HotDetailAdapter(mActivity, new ArrayList<Xiangxi>());
        mPullToRefreshListView.setAdapter(mHotDetailAdapter);
        mXiangxiRequest = new XiangxiRequest(new ApiListenrDefaultImpl()
        {
            @Override public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                XiangxiResponse xiangxiResponse = (XiangxiResponse) b5mBaseResponse;
                product = xiangxiResponse.product;
                initHeadView(product);
                mHotDetailAdapter.setList(xiangxiResponse.xiangxi);
                mHotDetailAdapter.notifyDataSetChanged();
            }
        }, mProgressBar);
        mXiangxiRequest.id = product.id;
    }

    private void initHeadView(Product product)
    {
        // TODO Auto-generated method stub
        ImageView iv_logo = (ImageView) headView.findViewById(R.id.iv_logo);
        TextView tv_score = (TextView) headView.findViewById(R.id.tv_score);
        RatingBar ratingBar = (RatingBar) headView.findViewById(R.id.rb);
        TextView tv_count = (TextView) headView.findViewById(R.id.tv_count);
        TextView tv_address = (TextView) headView.findViewById(R.id.tv_address);
        TextView tv_tel = (TextView) headView.findViewById(R.id.tv_tel);
        Button bt_map = (Button) headView.findViewById(R.id.bt_map);
        Button bt_360 = (Button) headView.findViewById(R.id.bt_360);
        B5MBitmapHelper.displayDefaultNotPeopleNotRoundImage(product.thum_img, iv_logo);
        tv_score.setText(String.format("评分: %s分", product.score));
        ratingBar.setRating(Float.valueOf(product.level));
        tv_count.setText("点评: " + product.comments + "人");
        tv_address.setText("地址: " + product.addr);
        tv_tel.setText("电话: " + product.phone);
        bt_map.setOnClickListener(this);
        bt_360.setOnClickListener(this);
    }

    @Override public void doAfter()
    {
        // TODO Auto-generated method stub
        mXiangxiRequest.start();
    }

    @Override public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.bt_map:
                Bundle mapBundle = new Bundle();
                mapBundle.putDouble("lat", Double.valueOf(product.latitude));
                mapBundle.putDouble("lng", Double.valueOf(product.longitude));
                B5MActivityHelper.startActivity(mActivity, MapActivity.class, mapBundle);
                break;
            case R.id.bt_360:
                Bundle bundle = new Bundle();
                bundle.putString("url", product.video);
                B5MActivityHelper.startActivity(mActivity, VideoActivity.class, bundle);
                break;
            default:
                break;
        }
    }
}
